<?php

namespace app\admin\model;

use think\Model;
use fast\Tree;

class Coursetype extends Model
{
    // 表名
    protected $name = 'course_type';
    protected $prefix_new = 'fa_';
    
    // 自动写入时间戳字段
    //protected $autoWriteTimestamp = 'datetime';
    //protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'delete_time';

    // 追加属性
    protected $append = [
        'status_text',
        'is_active_text'
    ];

    public function getStatusList()
    {
        return ['0' => __('禁用'), '1' => __('正常')];
    }

    public function getIsActiveList()
    {
        return ['0' => __('禁用'), '1' => __('启用')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getIsActiveTextAttr($value, $data)
    {
        $value = $value ? $value : $data['is_active'];
        $list = $this->getIsActiveList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDeleteTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    /**
     * 获取父级名称
     */
    public function getParentIdTextAttr($value, $data)
    {
        if ($data['parent_id'] == 0) {
            return '顶级分类';
        }
        $parent = $this->where('id', $data['parent_id'])->find();
        return $parent ? $parent['name'] : '';
    }

    /**
     * 获取树形结构数据
     */
    public function getTreeList($pid = 0)
    {
        $list = $this->where('status', 1)->order('weigh desc, id asc')->select();
        $list = collection($list)->toArray();
        foreach ($list as &$item) {
            $item['pid'] = isset($item['parent_id']) ? $item['parent_id'] : 0;
        }
        $tree = Tree::instance();
        $tree->init($list, 'pid');
        return $tree->getTreeList($tree->getTreeArray($pid), 'name');
    }

    /**
     * 获取所有子级ID
     */
    public function getChildrenIds($id)
    {
        $list = $this->where('status', 1)->select();
        $list = collection($list)->toArray();
        foreach ($list as &$item) {
            $item['pid'] = isset($item['parent_id']) ? $item['parent_id'] : 0;
        }
        $tree = Tree::instance();
        $tree->init($list, 'pid');
        return $tree->getChildrenIds($id);
    }

    /**
     * 获取所有父级ID
     */
    public function getParentIds($id)
    {
        $list = $this->where('status', 1)->select();
        $list = collection($list)->toArray();
        foreach ($list as &$item) {
            $item['pid'] = isset($item['parent_id']) ? $item['parent_id'] : 0;
        }
        $tree = Tree::instance();
        $tree->init($list, 'pid');
        return $tree->getParentsIds($id);
    }
} 